<?php

/*
 * litchicms
 * Copyright (C) 2010-5-26 Author: tiefa(539247@qq.com)
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
!defined('BRK') && exit("!");
check_rights_by_id(20);
$can_action = array('from', 'insert');

if (empty($_POST['action']) || !in_array($_POST['action'], $can_action)) {
    $_POST['action'] = 'from';
}

switch ($_POST['action']) {
    case 'from':
        $GROUP = brk_get_group();
        break;
    case 'insert':
        if (empty($_POST['user_login'])) {
            eMsg(0, "要添加的用户名不可以为空");
            $_POST['action'] = 'error';
            break;
        }
        if (empty($_POST['user_pass'])) {
            eMsg(0, "要添加的用户密码不可以为空");
            $_POST['action'] = 'error';
            break;
        }
        if (empty($_POST['group_id'])) {
            eMsg(0, "选择的组不可以为空");
            $_POST['action'] = 'error';
            break;
        }

        $rs = brk_select('group', "count(*) as num", " WHERE group_id='" . $_POST['group_id'] . "'");
        if (!($row = $Db->fetch_array($rs)) || $row['num'] == 0) {
            eMsg(0, "选择的权限组不存在");
            $_POST['action'] = 'error';
            break;
        }

        $rs = brk_select('users', "count(*) as num", " WHERE user_login='" . $_POST['user_login'] . "'");
        if (($row = $Db->fetch_array($rs)) && $row['num'] != 0) {
            eMsg(0, "添加用户失败&nbsp;" . $_POST['user_login'] . "&nbsp;已经存在");
            $_POST['action'] = 'error';
            break;
        }

        $data = array(
            'group_id' => $_POST['group_id'],
            'user_login' => $_POST['user_login'],
            'user_pass' => sha1($_POST['user_pass']),
            'user_registered' => date("Y-m-d H:i:s", $GLOBALS['BRK_GLOBAL']['datearray'][0]),
        );
        if (!brk_insert('users', $data)) {
            eMsg(0, "添加用户失败.");
            $_POST['action'] = 'error';
            break;
        }

        eMsg(1, "添加用户成功.");


        break;
    default:
        break;
}
?>
